package com.bytezero.webviewtest2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpActivity extends AppCompatActivity {
    TextView responseText, responseTextokhttp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);

        Button button = (Button) findViewById(R.id.btn_http);
        Button button1 = findViewById(R.id.btn_okhttp);

        responseText = findViewById(R.id.response_text);
        responseTextokhttp = findViewById(R.id.response_text_okhttp);

        button.setOnClickListener(v -> {
            sendRequestWithHttpURLConnection();
        });

        button1.setOnClickListener(v -> {
            sendRequestWithOKHttp();

        });
    }

    private void sendRequestWithHttpURLConnection() {
        //开启线程发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try {
                    URL url = new URL("https://www.baidu.com");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);

                    //提交数据
//                    connection.setRequestMethod("POST");
//                    DataOutputStream out= new DataOutputStream(connection.getOutputStream());
//                    out.writeBytes("username=admin&password=123546");

                    InputStream in = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder respose = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        respose.append(line);
                    }
                    showResponse(respose.toString());
                } catch (MalformedURLException | ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                responseText.setText(response);
            }
        });
    }

    //OKHttp
    private void sendRequestWithOKHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://y.qq.com")
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    showResponseOKHttp(responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void showResponseOKHttp(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                responseTextokhttp.setText(response);
            }
        });
    }
}